package controller;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import multiformat.Calculator;
import multiformat.FixedPointFormat;
import multiformat.FloatingPointFormat;
import multiformat.RationalFormat;

public class FormatController extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;
	private static final String[] bases = {"FixedPoint","FloatingPoint","Rational"};
	
	private JComboBox<String> cmbBases;
	private JLabel lblTitle;
	private Calculator model;
	
	public FormatController(Calculator model) {
		this.model = model;
		
		this.setLayout(new BorderLayout());
				
		this.lblTitle = new JLabel("<html><u>Format:</u></html>");
		this.lblTitle.setForeground(Color.red);
		
		this.cmbBases = new JComboBox<String>(FormatController.bases);
		this.cmbBases.setSelectedIndex(0);
		
		this.add(lblTitle,BorderLayout.NORTH);
		this.add(cmbBases,BorderLayout.CENTER);
		
		this.cmbBases.addActionListener(this);
		
		this.validate();
		this.setVisible(true);
	}


	@SuppressWarnings("unchecked")
	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		JComboBox<String> selected = null;
		
		if(source instanceof JComboBox<?>){
	        selected = (JComboBox<String>) source;
	        int index = selected.getSelectedIndex();
	        
	      	if(index == 0) {
				//FixedFormat selected
				model.setFormat(new FixedPointFormat());
				
			}else if(index == 1){
				//Binary selected
				model.setFormat(new FloatingPointFormat());			
			}else if(index == 2){
				//Hex selected
				model.setFormat(new RationalFormat());
			}
		}else{
			// Do nothing.
		}
	}

}
